package com.py.credit.utils;

import com.py.credit.entity.TreeObj;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Component
public class DeptTreeUtil extends TreeUtil {

    @Override
    public void newTreeObject(List<TreeObj> dataList, List<TreeObj> srcList, Map<String, TreeObj> cleanMap) {
        TreeObj r1 = null;
        for (TreeObj r : dataList) {
            r1 = new TreeObj();
            r1.setId(r.getId());
            r1.setName(r.getName());
            r1.setpId(r.getpId());
            r1.setSort(r.getSort());
            r1.setUrl(r.getUrl());
            r1.setType(r.getType());
            r1.setChecked(r.getChecked());
            r1.setNocheck(r.getNocheck());

            if ("D".equalsIgnoreCase(r.getType()) || "L".equalsIgnoreCase(r.getType())
                    || "T".equalsIgnoreCase(r.getType()) || "K".equalsIgnoreCase(r.getType())) {
                r1.setIsParent("true");
            }
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            StringBuffer path = new StringBuffer(200);
            path.append(request.getScheme());
            path.append("://");
            path.append(request.getServerName());
            path.append(":");
            path.append(request.getServerPort());
            path.append(request.getContextPath());

            if ("L".equalsIgnoreCase(r.getType()) || "D".equalsIgnoreCase(r.getType())) {
                r1.setIcon(path.toString() + "/images/icon/fenlei.png");
            }
            if ("K".equalsIgnoreCase(r.getType())) {
                r1.setIcon(path.toString() + "/images/icon/ku.png");
            }
            if ("T".equalsIgnoreCase(r.getType())) {
                r1.setIcon(path.toString() + "/images/icon/biao.png");
            }
            if ("M".equalsIgnoreCase(r.getType())) {
                r1.setIcon(path.toString() + "/images/icon/zhibiao.png");
            }

            srcList.add(r1);
            cleanMap.put(r.getId(), r1);
        }
    }

}
